package com.ysxsoft.common_base.view.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ysxsoft.common_base.R;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.view.custom.CirclePercentView;

import java.util.ArrayList;
import java.util.List;

public class CirclePercentGroupView extends FrameLayout {
    private Context mContext;
    private List<Data> data = new ArrayList<>();
    private RBaseAdapter<Data> adapter;

    public CirclePercentGroupView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public CirclePercentGroupView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CirclePercentGroupView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        removeAllViews();
        mContext = context;
        RecyclerView recyclerView = new RecyclerView(mContext);
        LinearLayoutManager manager=new LinearLayoutManager(mContext);
        manager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(manager);
        adapter = new RBaseAdapter<Data>(mContext, R.layout.item_circle_percent, data) {
            @Override
            protected void fillItem(RViewHolder holder, Data item, int position) {
                CirclePercentView circlePercentView=holder.getView(R.id.circlePercentView);
                circlePercentView.setPercentAndColor(item.getPercent(),getResources().getColor(item.getColor()));

                TextView text=holder.getView(R.id.text);
                text.setText(item.getName());
            }

            @Override
            protected int getViewType(Data item, int position) {
                return 0;
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        addView(recyclerView);
    }

    public void setData(List<Data> datas) {
        this.data.clear();
        this.data.addAll(datas);
        adapter.notifyDataSetChanged();
    }

    public void setDebugData() {
        this.data.clear();
        this.data.addAll(getTestData());
        adapter.notifyDataSetChanged();
    }

    private static int[] color = new int[]{R.color.colorPrimary,
            R.color.color_5CF0C1,
            R.color.color_5D90FD,
            R.color.color_FB9123,
            R.color.color_ffac12,
            R.color.possible_result_points,
            R.color.colorRed,
            R.color.hint
    };

    public static List<Data> getTestData() {
        List<Data> data = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            int c = color[i % 7];
            data.add(new Data("id" + i, "分组"+i, (int)i*10, c));
        }
        return data;
    }

    public static class Data {
        private String id;
        private String name;
        private int percent;
        private int color;

        public Data(String id, String name, int percent, int color) {
            this.id = id;
            this.name = name;
            this.percent = percent;
            this.color = color;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getPercent() {
            return percent;
        }

        public void setPercent(int percent) {
            this.percent = percent;
        }
    }
}
